/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.library.web_layer.readerPages;

import cz.muni.fi.pa165.library.api.ReservationService;
import cz.muni.fi.pa165.library.api.ReservationTO;
import cz.muni.fi.pa165.library.web_layer.ApplicationContextProvider;
import cz.muni.fi.pa165.library.web_layer.AuthenticatedSession;
import cz.muni.fi.pa165.library.web_layer.HomePage;
import cz.muni.fi.pa165.library.web_layer.Template;
import cz.muni.fi.pa165.library.web_layer.adminPages.Reader.ShowAllReader;
import cz.muni.fi.pa165.library.web_layer.adminPages.reservation.ShowAllReservation;
import java.text.SimpleDateFormat;
import java.util.List;
import org.apache.wicket.Session;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.AbstractItem;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.StringValue;

/**
 *
 * @author dulinka
 */
public class BookReservation extends Template {
        private static final long serialVersionUID = 1L; 
    
    public BookReservation(final PageParameters parameters) {
	super(parameters);
        
        RepeatingView repeating = new RepeatingView("repeating");
        add(repeating);
        ReservationService reservationService = (ReservationService) ApplicationContextProvider.getApplicationContext().getBean("reservationService");
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        AuthenticatedSession session = ((AuthenticatedSession)Session.get());
        List<ReservationTO> list = reservationService.findReservationsByReader(session.getReader());
        if(list != null){
            for (ReservationTO reservation : list) {
                AbstractItem item = new AbstractItem(repeating.newChildId());
                PageParameters pageParameters = new PageParameters();
                item.add(new BookmarkablePageLink<Void>("delete", ShowAllReservation.class, pageParameters));
                pageParameters.add("reservationid", reservation.getReservationID());
                item.add(new Label("author", reservation.getBookTO().getAuthor()));
                item.add(new Label("title", reservation.getBookTO().getTitle()));
                item.add(new Label("reservationDate", dateFormat.format(reservation.getReservationDate())));
                repeating.add(item);
            }
        }
        
        //delete reservation
        StringValue reservationId = parameters.get("reservationid");
        if (!reservationId.isEmpty()) {
            reservationService.deleteReservation(reservationService.findReservationByID(reservationId.toLong()));
            setResponsePage(BookReservation.class);
        }
    }
}